package com.xzy.urp.listener;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.xzy.urp.mapper.TeacherInfoMapper;
import com.xzy.urp.mapper.UserInfoMapper;
import com.xzy.urp.pojo.dto.ExcelTeacherDTO;
import com.xzy.urp.pojo.dto.ExcelUserDTO;
import com.xzy.urp.pojo.entity.TeacherInfo;
import com.xzy.urp.pojo.entity.UserInfo;
import com.xzy.urp.pojo.vo.TeacherInfoVO;
import com.xzy.urp.service.TeacherInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class TeacherExcelListener  extends AnalysisEventListener<ExcelTeacherDTO> {


    private TeacherInfoService service;



    //构造器注入属性
    public TeacherExcelListener(TeacherInfoService service){
        this.service = service;
    }


    @Override
    public void invoke(ExcelTeacherDTO data, AnalysisContext context) {

        TeacherInfoVO teacherInfoVO = new TeacherInfoVO();
        BeanUtil.copyProperties(data,teacherInfoVO);
        teacherInfoVO.setUId(data.getId());

        try{
            service.addTInfo(teacherInfoVO) ;
        }catch (DuplicateKeyException e){
            log.error("用户已存在"+teacherInfoVO.getUId());
            //todo: 标记出现异常
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // TODO: 判断是否出现异常并前端返回
    }
}
